﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.WinForm;

namespace Kugar.Core.ExtMethod
{
    public static class ComboFunction
    {
        /// <summary>
        ///     通过Combo中显示的文本选中符合的第一项
        /// </summary>
        /// <param name="cmb">要查找的下拉框</param>
        /// <param name="text">显示的文本</param>
        /// <returns>返回指定项的index ,-1为找不到指定项目</returns>
        public static int ComboSelectByText(this ComboBox cmb, string text)
        {
            if (cmb == null || cmb.Items.Count <= 0)
            {
                return -1;
            }

            for (int i = 0; i < cmb.Items.Count; i++)
            {
                if (cmb.Items[i].ToString() == text)
                {
                    return i;
                }
            }

            return -1;
        }

        public static int ComboSelectByValue(this ComboBox cmb, object value)
        {
            if (cmb == null || cmb.Items.Count <= 0)
            {
                return -1;
            }

            for (int i = 0; i < cmb.Items.Count; i++)
            {
                if (cmb.Items[i] is ComboItemEx)
                {
                    var v = (ComboItemEx)cmb.Items[i];

                    if (v != null && v.Value.ToString() == value.ToString())
                    {
                        return i;
                    }
                }


            }

            return -1;
        }

    }



}
